using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Common.Utilities.DLR;
using Workflows.Components;
using Workflows.Components.DALC;

namespace Workflows.Diagrams
{
    public delegate void DataConverterUpdated(
        FlowLink link, Connector wfConnector, WorkflowComponentBase toComponent, List<CodeFieldMapper> fieldMappers);

    public partial class DataPipeMapper : Form
    {
        #region fields
        public event DataConverterUpdated OnDataConverterUpdated;

        private FlowLink _Link;
        private Connector _WfConnector;
        private string _ToPortName;
        private WorkflowComponentBase _FromComponent;
        private WorkflowComponentBase _ToComponent;
        private Dictionary<string, string> _FieldTransformationCode;
        #endregion

        #region init
        public DataPipeMapper()
        {
            InitializeComponent();
        }

        private void DataPipeMapper_Load(object sender, EventArgs e)
        {
            this.PopulateEvaluationOperators();
            this.PopulateEvalOperatorInDataGridView();

            if (this._ToComponent != null && this._ToComponent.GetType() == typeof(Subflow))
            {
                this.lbl_SrcCollectionData.Visible = true;
                this.lbl_TgtItemData.Visible = true;
                this.cbo_SrcCollectionData.Visible = true;
                this.cbo_TgtItemData.Visible = true;
            }
            else
            {
                this.lbl_SrcCollectionData.Visible = false;
                this.lbl_TgtItemData.Visible = false;
                this.cbo_SrcCollectionData.Visible = false;
                this.cbo_TgtItemData.Visible = false;
            }
        }

        private void PopulateEvaluationOperators()
        {
            this.cbo_EvaOp.Items.Clear();
            for(int i=0;i<=6;i++)
            {
                EvaluationOperator evaOp = (EvaluationOperator) i;
                this.cbo_EvaOp.Items.Add(evaOp.ToString());
            }
        }

        private void PopulateEvalOperatorInDataGridView()
        {
            foreach(DataGridViewRow dr in this.dv_Evaluators.Rows)
            {
                if(dr.IsNewRow)
                {
                    DataGridViewComboBoxCell cellOp = (DataGridViewComboBoxCell) dr.Cells[1];
                    cellOp.Items.Clear();
                    for(int i=0;i<=6;i++)
                    {
                        cellOp.Items.Add(((EvaluationOperator) i).ToString());
                    }
                }
            }
        }

        public void SetDataPipe(FlowLink link, Connector wfConnector, string toPortName, WorkflowComponentBase fromComponent, WorkflowComponentBase toComponent)
        {
            this._Link = link;
            this._WfConnector = wfConnector;
            this._ToPortName = toPortName;
            this._FromComponent = fromComponent;
            this._ToComponent = toComponent;
            if(this._ToComponent.GetType()==typeof(Subflow))
            {
                this.lbl_SrcCollectionData.Visible = true;
                this.lbl_TgtItemData.Visible = true;
                this.cbo_SrcCollectionData.Visible = true;
                this.cbo_TgtItemData.Visible = true;
            }
            this._FieldTransformationCode = new Dictionary<string, string>();
            this.PopulateFieldBindings();
            this.DisplayFilter();
        }

        public void SetFieldMappers(List<CodeFieldMapper> fieldMappers)
        {
            if (fieldMappers.Count == 1 && string.IsNullOrEmpty(fieldMappers[0].SrcFieldName) && string.IsNullOrEmpty(fieldMappers[0].TgtFieldName))
            {
                this.rd_Direct.Checked = true;
            }
            else
            {
                this.rd_Indirect.Checked = true;
                if (fieldMappers.Count > 0)
                {
                    foreach (CodeFieldMapper fieldMapper in fieldMappers)
                    {
                        if (this._FieldTransformationCode.ContainsKey(fieldMapper.TgtFieldName))
                        {
                            this._FieldTransformationCode[fieldMapper.TgtFieldName] =
                                fieldMapper.AssignmentTransformation;
                        }
                        else
                        {
                            this._FieldTransformationCode.Add(fieldMapper.TgtFieldName,
                                                              fieldMapper.AssignmentTransformation);
                        }
                    }

                    foreach (DataGridViewRow drMapping in this.dv_FieldMappings.Rows)
                    {
                        string tgtFieldName = drMapping.Cells[0].Value.ToString();
                        
                        foreach (CodeFieldMapper fieldMapper in fieldMappers)
                        {
                            if (fieldMapper.TgtFieldName == tgtFieldName)
                            {
                                string srcFieldName = fieldMapper.SrcFieldName;
                                DataGridViewComboBoxCell cellSrc = (DataGridViewComboBoxCell) drMapping.Cells[1];
                                cellSrc.Value = srcFieldName;

                                
                                DataGridViewImageCell cellImg = (DataGridViewImageCell) drMapping.Cells[2];
                                bool canMap = this.ValidateFieldMappingTypeCompatibility(srcFieldName, tgtFieldName);
                                if (canMap)
                                    cellImg.Value = this.imageList1.Images[1];
                                else
                                    cellImg.Value = this.imageList1.Images[2];
                                break;
                            }
                        }
                    }
                }

                if(this._ToComponent !=null && this._ToComponent.GetType()==typeof(Subflow))
                {
                    Subflow subflow = (Subflow) this._ToComponent;
                    SubflowInstruction instruction =(SubflowInstruction) subflow.Instruction;
                    this.cbo_TgtItemData.SelectedIndex = -1;
                    this.cbo_TgtItemData.Text = string.Empty;
                    if(!string.IsNullOrEmpty(instruction.IteratorDataName))
                    {
                        if(this.cbo_TgtItemData.Items.Count >0)
                        {
                            for(int i=0;i<this.cbo_TgtItemData.Items.Count;i++)
                            {
                                if(this.cbo_TgtItemData.Items[i].ToString()==instruction.IteratorDataName)
                                {
                                    this.cbo_TgtItemData.SelectedIndex = i;
                                    break;
                                }
                            }
                        }
                    }
                    this.cbo_SrcCollectionData.SelectedIndex = -1;
                    this.cbo_SrcCollectionData.Text = string.Empty;
                    if(!string.IsNullOrEmpty(instruction.IteratorDataCollectionName))
                    {
                        if(this.cbo_SrcCollectionData.Items.Count>0)
                        {
                            for(int i=0;i<this.cbo_SrcCollectionData.Items.Count;i++)
                            {
                                if(this.cbo_SrcCollectionData.Items[i].ToString()==instruction.IteratorDataCollectionName)
                                {
                                    this.cbo_SrcCollectionData.SelectedIndex = i;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        
        private void PopulateFieldBindings()
        {
            this.cbo_TgtItemData.Items.Clear();
            this.cbo_SrcCollectionData.Items.Clear();
            this.cbo_SrcCollectionData.Items.Add("Ignore");
            this.cbo_SrcField.Items.Clear();

            this.dv_FieldMappings.Rows.Clear();
            Dictionary<string, Type> fromMembers = new Dictionary<string, Type>();
            Dictionary<string, Type> toMembers = new Dictionary<string, Type>();

            
            if(this._FromComponent!=null && this._FromComponent.Output!=null)
            {
                Dictionary<string, Type> fromInnerMembers =
                    this._FromComponent.Output.GetBindableMemberNames(null);
                if (fromInnerMembers != null && fromInnerMembers.Count > 0)
                {
                    foreach (string key in fromInnerMembers.Keys)
                    {
                        fromMembers.Add(key, fromInnerMembers[key]);
                    }
                }
            }
            if (this._ToComponent != null && (this._ToComponent.Instruction !=null))
            {
                InstructionBase instruction = this._ToComponent.Instruction;
                if (instruction.GetType().Name.ToLower() == this._ToPortName.ToLower())
                {
                    Dictionary<string, Type> members = new Dictionary<string, Type>();
                    if(!this.ck_ShowInheritableMembers.Checked)
                    {
                        members = instruction.GetBindableMemberNames(new Type[] {typeof (InstructionBase)});
                    }
                    else
                    {
                        members = instruction.GetBindableMemberNames(null);
                    }
                    if (members != null && members.Count > 0)
                    {
                        foreach (string toFieldName in members.Keys)
                        {
                            if (toFieldName != "IteratorDataName" && toFieldName != "IteratorDataCollectionName")
                            {
                                if (!toMembers.ContainsKey(toFieldName))
                                {
                                    toMembers.Add(toFieldName, members[toFieldName]);
                                }
                            }
                        }
                    }
                }

                if(this._ToComponent.GetType()==typeof(Subflow))
                {
                    Subflow subflow = (Subflow) this._ToComponent;
                    WorkflowDesign subflowDesign = WorkflowDesignDALC.GetWorkflowDesign(subflow.SubFlowID);
                    if(subflowDesign.GlobalVariables !=null && subflowDesign.GlobalVariables.Length>0)
                    {
                        foreach(GlobalVariable wfVar in subflowDesign.GlobalVariables)
                        {
                            if (toMembers.ContainsKey(wfVar.VarName))
                            {
                                toMembers[wfVar.VarName] =
                                    SimpleDataTypeUtil.ToSystemType(wfVar.DataType);
                            }
                            else
                            {
                                toMembers.Add(wfVar.VarName, SimpleDataTypeUtil.ToSystemType(wfVar.DataType));
                            }
                        }
                    }
                }
            }

            if(fromMembers.Count>0)
            {
                foreach(string fromMemberName in fromMembers.Keys)
                {
                    this.cbo_SrcCollectionData.Items.Add(fromMemberName);
                    this.cbo_SrcField.Items.Add(fromMemberName);
                }
            }
            if(toMembers.Count>0)
            {
                foreach(string toMemberName in toMembers.Keys)
                {
                    this.cbo_TgtItemData.Items.Add(toMemberName);
                }
            }

            if(toMembers.Count>0)
            {
                foreach(string tgtFieldName in toMembers.Keys)
                {
                    DataGridViewRow drMapping=new DataGridViewRow();
                    DataGridViewTextBoxCell cellTgtField=new DataGridViewTextBoxCell();
                    cellTgtField.Value = tgtFieldName;
                    drMapping.Cells.Add(cellTgtField);

                    DataGridViewComboBoxCell cellSrcField=new DataGridViewComboBoxCell();
                    cellSrcField.Items.Add("Ignore");
                    foreach(string srcFieldName in fromMembers.Keys)
                    {
                        cellSrcField.Items.Add(srcFieldName);
                    }
                    drMapping.Cells.Add(cellSrcField);

                    DataGridViewImageCell cellTransform=new DataGridViewImageCell(false);
                    cellTransform.Value = this.imageList1.Images[0];
                    drMapping.Cells.Add(cellTransform);

                    this.dv_FieldMappings.Rows.Add(drMapping);
                }
                this.dv_FieldMappings.Refresh();
            }
        }

        private void DisplayFilter()
        {
            this.dv_Evaluators.Rows.Clear();
            if(this._WfConnector.Filter !=null && this._WfConnector.Filter.Length>0)
            {
                this.ck_EnableFilter.Checked = true;
                foreach(Evaluable eva in this._WfConnector.Filter)
                {
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewTextBoxCell cellField = new DataGridViewTextBoxCell();
                    cellField.Value = eva.FieldName;
                    dr.Cells.Add(cellField);
                    DataGridViewComboBoxCell cellOp = new DataGridViewComboBoxCell();
                    for (int i = 0; i <= 6; i++)
                    {
                        EvaluationOperator evalOp = (EvaluationOperator) i;
                        cellOp.Items.Add(evalOp.ToString());
                    }
                    cellOp.Value = eva.Operator.ToString();
                    dr.Cells.Add(cellOp);
                    DataGridViewTextBoxCell cellTgtValue = new DataGridViewTextBoxCell();
                    cellTgtValue.Value = eva.CompareAgainst.ToString();
                    dr.Cells.Add(cellTgtValue);
                    this.dv_Evaluators.Rows.Add(dr);
                }
            }
            else
            {
                this.ck_EnableFilter.Checked = false;
            }
        }
        #endregion

        private void btn_OK_Click(object sender, EventArgs e)
        {
            List<CodeFieldMapper> fieldMappers = new List<CodeFieldMapper>();
            if (this.rd_Direct.Checked)
            {
                CodeFieldMapper fieldMapper=new CodeFieldMapper();
                fieldMapper.AssignmentTransformation = string.Empty;
                fieldMapper.SrcComponentID = this._FromComponent.ComponentID;
                fieldMapper.SrcFieldName = string.Empty;
                fieldMapper.TgtComponentID = this._ToComponent.ComponentID;
                fieldMapper.TgtFieldName = string.Empty;
                fieldMapper.TgtInputName = this._ToPortName;
                fieldMappers.Add(fieldMapper);
            }
            else
            {
                if (this.dv_FieldMappings.Rows.Count > 0)
                {
                    foreach (DataGridViewRow drMapping in this.dv_FieldMappings.Rows)
                    {
                        if (drMapping.Cells[0] != null)
                        {
                            if (!drMapping.IsNewRow)
                            {
                                DataGridViewTextBoxCell cellTgt = (DataGridViewTextBoxCell) drMapping.Cells[0];
                                DataGridViewComboBoxCell cellSrc = (DataGridViewComboBoxCell) drMapping.Cells[1];
                                string tgtFieldName = (string) cellTgt.Value;
                                string srcFieldName = (string) cellSrc.Value;
                                if ((!string.IsNullOrEmpty(tgtFieldName)) && (!string.IsNullOrEmpty(srcFieldName)))
                                {
                                    if (srcFieldName == "Ignore")
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        CodeFieldMapper fieldMapper = new CodeFieldMapper();
                                        fieldMapper.SrcComponentID = this._FromComponent.ComponentID;
                                        fieldMapper.TgtComponentID = this._ToComponent.ComponentID;
                                        fieldMapper.TgtInputName = string.Empty;
                                        fieldMapper.TgtFieldName = tgtFieldName;
                                        fieldMapper.SrcFieldName = srcFieldName;
                                        fieldMapper.AssignmentTransformation = string.Empty;
                                        if (this._FieldTransformationCode.ContainsKey(tgtFieldName))
                                        {
                                            fieldMapper.AssignmentTransformation =
                                                this._FieldTransformationCode[tgtFieldName];
                                        }
                                        fieldMappers.Add(fieldMapper);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            List<Evaluable> evals=new List<Evaluable>();
            if(this.dv_Evaluators.Rows.Count>0)
            {
                foreach(DataGridViewRow dr in this.dv_Evaluators.Rows)
                {
                    if(!dr.IsNewRow)
                    {
                        DataGridViewTextBoxCell cellFieldName = (DataGridViewTextBoxCell) dr.Cells[0];
                        string fieldName = cellFieldName.Value.ToString();
                        SimpleDataType fieldType = SimpleDataTypeUtil.ToSimpleDataType(this.GetSrcType(fieldName));
                        DataGridViewComboBoxCell cellOp = (DataGridViewComboBoxCell) dr.Cells[1];
                        EvaluationOperator op = EvaluationOperator.Equals;
                        if(cellOp.Value !=null)
                        {
                            for(int i=0;i<=6;i++)
                            {
                                if(cellOp.Value.ToString()==((EvaluationOperator)i).ToString())
                                {
                                    op = (EvaluationOperator) i;
                                    break;
                                }
                            }
                        }
                        DataGridViewTextBoxCell cellTgtValue =(DataGridViewTextBoxCell) dr.Cells[2];
                        string tgtValueStr = string.Empty;
                        if (cellTgtValue.Value != null)
                            tgtValueStr = cellTgtValue.Value.ToString();
                        object tgtValue = SimpleDataTypeUtil.ToDbValue(fieldType, tgtValueStr);
                        Evaluable eval=new Evaluable(fieldName,fieldType,op,tgtValue);
                        evals.Add(eval);
                    }
                }
            }

            if(this.OnDataConverterUpdated !=null)
            {
                //Type fromType = this._FromComponent.Output.GetType();
                //Type toType = Type.Missing.GetType();

                if (this._ToComponent != null && (this._ToComponent.Instruction !=null))
                {
                    //InstructionBase inputType = this._ToComponent.Instruction;
                    //if (inputType.GetType().Name.ToLower() == this._ToPortName.ToLower())
                    //{
                    //    toType = inputType.GetType();
                    //}

                    if(this._ToComponent.GetType()==typeof(Subflow))
                    {
                        SubflowInstruction subflowInstruction =
                            (SubflowInstruction) ((Subflow) this._ToComponent).Instruction;
                        subflowInstruction.IteratorDataCollectionName = string.Empty;
                        if(this.cbo_SrcCollectionData.SelectedIndex>=0)
                        {
                            string srcCollDataName = this.cbo_SrcCollectionData.SelectedItem.ToString();
                            if(srcCollDataName !="Ignore")
                            {
                                subflowInstruction.IteratorDataCollectionName = srcCollDataName;
                            }
                        }
                        subflowInstruction.IteratorDataName = string.Empty;
                        if(this.cbo_TgtItemData.SelectedIndex >=0)
                        {
                            subflowInstruction.IteratorDataName = this.cbo_TgtItemData.SelectedItem.ToString();
                        }
                    }
                }

                this._WfConnector.FieldMappers = fieldMappers;
                this._WfConnector.Filter = null;
                if(evals.Count>0)
                {
                    this._WfConnector.Filter = evals.ToArray();
                }

                this.OnDataConverterUpdated(this._Link, this._WfConnector, this._ToComponent, fieldMappers);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rd_Direct_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rd_Direct.Checked)
            {
                this.lbl_Direct.Enabled = true;
                this.dv_FieldMappings.Enabled = false;
            }
            else
            {
                this.lbl_Direct.Enabled = false;
                this.dv_FieldMappings.Enabled = true;
            }
        }

        private void rd_Indirect_CheckedChanged(object sender, EventArgs e)
        {
            if(this.rd_Direct.Checked)
            {
                this.lbl_Direct.Enabled = true;
                this.dv_FieldMappings.Enabled = false;
            }
            else
            {
                this.lbl_Direct.Enabled = false;
                this.dv_FieldMappings.Enabled = true;
            }
        }

        #region type checking
        private void dv_FieldMappings_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
        {
            if(e.Cell !=null && e.Cell.RowIndex >=0 && e.Cell.ColumnIndex >=0)
            {
                int rowIdx = e.Cell.RowIndex;
                DataGridViewRow dr = this.dv_FieldMappings.Rows[rowIdx];
                if(!dr.IsNewRow && dr.Cells[0].Value !=null && dr.Cells[1].Value !=null)
                {
                    DataGridViewImageCell cell = (DataGridViewImageCell)dr.Cells[2];
                    string tgtFieldName = dr.Cells[0].Value.ToString();
                    string srcFieldName = dr.Cells[1].Value.ToString();
                    if ((!string.IsNullOrEmpty(tgtFieldName)) && (!string.IsNullOrEmpty(srcFieldName)))
                    {
                        if (srcFieldName != "Ignore")
                        {
                            bool canMap = this.ValidateFieldMappingTypeCompatibility(srcFieldName, tgtFieldName);

                            if (canMap)
                            {
                                cell.Value = this.imageList1.Images[1];
                            }
                            else
                            {
                                cell.Value = this.imageList1.Images[2];
                            }
                        }
                        else
                        {
                            cell.Value = this.imageList1.Images[0];
                        }
                    }
                    else
                    {
                        cell.Value = this.imageList1.Images[0];
                    }
                }
                else
                {
                    dr.Cells[2].Value = this.imageList1.Images[0];
                }
            }
        }
        
        private bool ValidateFieldMappingTypeCompatibility(string srcFieldName, string tgtFieldName)
        {
            Type srcType = this.GetSrcType(srcFieldName);
            Type tgtType = this.GetTgtType(tgtFieldName);
            if(srcType==tgtType)
                return true;
            else
                return false;
        }

        private Type GetSrcType(string srcFieldName)
        {
            Dictionary<string, Type> fromInnerMembers = this._FromComponent.Output.GetBindableMemberNames(null);
            if(fromInnerMembers !=null && fromInnerMembers.ContainsKey(srcFieldName))
            {
                return fromInnerMembers[srcFieldName];
            }
            else
            {
                return Type.Missing.GetType();
            }
        }

        private Type GetTgtType(string tgtFieldName)
        {
            if(this._ToComponent.GetType()==typeof(Subflow))
            {
                Subflow subflow = (Subflow) this._ToComponent;
                WorkflowDesign wfDesign = WorkflowDesignDALC.GetWorkflowDesign(subflow.SubFlowID);
                if(wfDesign.GlobalVariables !=null && wfDesign.GlobalVariables.Length>0)
                {
                    foreach(GlobalVariable wfVar in wfDesign.GlobalVariables)
                    {
                        if(wfVar.VarName==tgtFieldName)
                        {
                            return SimpleDataTypeUtil.ToSystemType(wfVar.DataType);
                        }
                    }
                }
            }

            Dictionary<string, Type> members = new Dictionary<string, Type>();
            if (!this.ck_ShowInheritableMembers.Checked)
            {
                members = this._ToComponent.Instruction.GetBindableMemberNames(new Type[] {typeof (InstructionBase)});
            }
            else
            {
                members = this._ToComponent.Instruction.GetBindableMemberNames(null);
            }
            if(members !=null && members.ContainsKey(tgtFieldName))
            {
                return members[tgtFieldName];
            }
            else
            {
                return Type.Missing.GetType();
            }
        }
        #endregion

        #region transformation
        private void dv_FieldMappings_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex==2)
            {
                DataGridViewRow dr = this.dv_FieldMappings.Rows[e.RowIndex];
                if (!dr.IsNewRow && dr.Cells[0] != null && dr.Cells[1] != null && dr.Cells[0].Value != null && dr.Cells[1].Value != null)
                {
                    string tgtFieldName = dr.Cells[0].Value.ToString();
                    string srcFieldName = dr.Cells[1].Value.ToString();
                    if ((!string.IsNullOrEmpty(tgtFieldName)) && (!string.IsNullOrEmpty(srcFieldName)))
                    {
                        if (srcFieldName != "Ignore")
                        {
                            Type srcType = this.GetSrcType(srcFieldName);
                            Type tgtType = this.GetTgtType(tgtFieldName);
                            string codeSnippet = string.Empty;
                            if(this._FieldTransformationCode !=null && this._FieldTransformationCode.ContainsKey(tgtFieldName))
                            {
                                codeSnippet = this._FieldTransformationCode[tgtFieldName];
                            }
                            ConverterFactory factory = new ConverterFactory(e.RowIndex, srcType, tgtType, codeSnippet);
                            factory.CodeSnippetUpdatedEvent += new CodeSnippetUpdated(factory_CodeSnippetUpdatedEvent);
                            factory.Show(this);
                        }
                    }
                }
            }
        }

        void factory_CodeSnippetUpdatedEvent(int rowIndex, string code)
        {
            DataGridViewRow dr = this.dv_FieldMappings.Rows[rowIndex];
            if (!dr.IsNewRow && dr.Cells[0] != null && dr.Cells[1] != null && dr.Cells[0].Value != null && dr.Cells[1].Value != null)
            {
                string tgtFieldName = dr.Cells[0].Value.ToString();
                if(this._FieldTransformationCode.ContainsKey(tgtFieldName))
                {
                    this._FieldTransformationCode[tgtFieldName] = code;
                }
                else
                {
                    this._FieldTransformationCode.Add(tgtFieldName,code);
                }
            }
        }

        #endregion

        private void ck_ShowInheritableMembers_CheckedChanged(object sender, EventArgs e)
        {
            this.PopulateFieldBindings();
        }

        #region subflow iterator binding
        private void cbo_SrcCollectionData_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.UpdateSubflowIteratorDataBindings();
        }

        private void cbo_TgtItemData_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.UpdateSubflowIteratorDataBindings();
        }

        private void UpdateSubflowIteratorDataBindings()
        {
            if (this.cbo_SrcCollectionData.SelectedIndex >= 0 && this.cbo_TgtItemData.SelectedIndex >= 0)
            {
                string tgtFieldName = this.cbo_TgtItemData.SelectedItem.ToString();
                string srcFieldName = this.cbo_SrcCollectionData.SelectedItem.ToString();
                if (srcFieldName == "Ignore")
                {
                    foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
                    {
                        dr.Visible = true;
                    }
                }
                else
                {
                    foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
                    {
                        if (!dr.IsNewRow)
                        {
                            if (dr.Cells[0].Value != null)
                            {
                                if (tgtFieldName == dr.Cells[0].Value.ToString())
                                {
                                    DataGridViewComboBoxCell cellSrc = (DataGridViewComboBoxCell) dr.Cells[1];
                                    cellSrc.Value = this.cbo_SrcCollectionData.SelectedItem.ToString();

                                    dr.Visible = false;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
                {
                    dr.Visible = true;
                }
            }
        }
        #endregion

        private void ck_EnableFilter_CheckedChanged(object sender, EventArgs e)
        {
            if(this.ck_EnableFilter.Checked)
            {
                this.cbo_SrcField.Enabled = true;
                this.cbo_EvaOp.Enabled = true;
                this.txt_EvaTgtValue.Enabled = true;
                this.btn_AddEvaluator.Enabled = true;
                this.dv_Evaluators.Enabled = true;
            }
            else
            {
                this.cbo_SrcField.Enabled = false;
                this.cbo_EvaOp.Enabled = false;
                this.txt_EvaTgtValue.Enabled = false;
                this.btn_AddEvaluator.Enabled = false;
                this.dv_Evaluators.Enabled = false;
            }
        }

        private void btn_AddEvaluator_Click(object sender, EventArgs e)
        {
            if(this.cbo_SrcField.SelectedIndex>=0 && this.cbo_EvaOp.SelectedIndex>=0 && (!string.IsNullOrEmpty(this.txt_EvaTgtValue.Text)))
            {
                string srcFieldName = this.cbo_SrcField.SelectedItem.ToString();
                SimpleDataType simpleType = SimpleDataTypeUtil.ToSimpleDataType(this.GetSrcType(srcFieldName));
                EvaluationOperator op = (EvaluationOperator) this.cbo_EvaOp.SelectedIndex;
                object tgtValue = SimpleDataTypeUtil.ToDbValue(simpleType, this.txt_EvaTgtValue.Text);
                DataGridViewRow dr=new DataGridViewRow();
                DataGridViewTextBoxCell cellField= new DataGridViewTextBoxCell();
                cellField.Value = srcFieldName;
                dr.Cells.Add(cellField);
                DataGridViewComboBoxCell cellOp=new DataGridViewComboBoxCell();
                for(int i=0;i<=6;i++)
                {
                    EvaluationOperator evalOp = (EvaluationOperator) i;
                    cellOp.Items.Add(evalOp.ToString());
                }
                cellOp.Value = op.ToString();
                dr.Cells.Add(cellOp);
                DataGridViewTextBoxCell cellTgtValue=new DataGridViewTextBoxCell();
                cellTgtValue.Value = tgtValue.ToString();
                dr.Cells.Add(cellTgtValue);
                this.dv_Evaluators.Rows.Add(dr);

                this.dv_Evaluators.Refresh();
            }
        }

        private void dv_Evaluators_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            this.PopulateEvalOperatorInDataGridView();
        }
    }
}